/**
 * <copyright>
 * Copyrigth 2007 Aurelien Moreau
 *
 * This file is part of ProcessShop.
 *
 * ProcessShop is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * ProcessShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with ProcessShop; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * </copyright>
 *
 * $Id$
 */
package net.yienyien.process.bpmn2bpel.wizards;

import java.util.ArrayList;

import net.yienyien.process.bpd.BPDModel;
import net.yienyien.process.bpd.transducer.BPDLoaderUtil;
import net.yienyien.process.bpd.transducer.ProviderException;
import net.yienyien.process.pim.ProcessInformation;
import net.yienyien.process.pmm.Binding;
import net.yienyien.process.pmm.Bindings;



import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.stp.bpmn.BpmnDiagram;
import org.eclipse.stp.bpmn.Pool;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * The "New" wizard page allows setting the container for the new file as well
 * as the file name. The page will only accept file name without the extension
 * OR with the extension that matches the expected one (bpel).
 */

public class SampleNewWizardPage extends WizardPage {
	private Text bpmnText;
	private Combo poolNames;
	private ArrayList<Pool> pools;
	private Text pimText;
	private Text pmmText;
	private Text outText;
	private BpmnDiagram diag; 
	private ResourceSet set=new ResourceSetImpl();
	private Button pmmLoad;
	
	private BPDModel bpd=null;
	private Bindings pmm=null;
	private ProcessInformation pim=null;


	public BPDModel getBPD() {
		return bpd;
	}
	
	public Bindings getPMM() {
		return pmm;
	}
	
	public ProcessInformation getPIM() {
		return pim;
	}
	
	public String getOutFile() {
		return outText.getText();
	}
	
	/**
	 * Constructor for SampleNewWizardPage.
	 * 
	 * @param pageName
	 */
	public SampleNewWizardPage() {
		super("wizardPage");
		setTitle("BPEL Generator");
		setDescription("This wizard creates a new file with *.bpel extension.");
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {		
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 4;
		
		/* BPMN */
		Label bpmnLabel = new Label(container, SWT.NULL);
		bpmnLabel.setText("&BPMN:");

		bpmnText = new Text(container, SWT.BORDER | SWT.SINGLE);
		
		Button bpmnBrowse = new Button(container, SWT.PUSH);
		bpmnBrowse.setText("Browse");
		bpmnBrowse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				browse(bpmnText, "*.bpmn");
			}
		});
		
		
		Button bpmnLoad = new Button(container, SWT.PUSH);
		bpmnLoad.setText("Load");
		bpmnLoad.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				bpmnLoad();
			}});
		
		
		/* POOL */
		Label poolLabel = new Label(container, SWT.NULL);
		poolLabel.setText("&POOL:");
		
		poolNames=new Combo(container, SWT.BORDER | SWT.V_SCROLL);
		
		Button poolBrowse = new Button(container, SWT.PUSH);
		poolBrowse.setText("");
		poolBrowse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				browse(null, null);
			}
		});
		
		Button poolLoad = new Button(container, SWT.PUSH);
		poolLoad.setText("Load");
		poolLoad.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				poolLoad();
			}});
		
		/* PIM */
		Label pimLabel = new Label(container, SWT.NULL);
		pimLabel.setText("&PIM:");
		
		pimText = new Text(container, SWT.BORDER | SWT.SINGLE);
		
		Button pimBrowse = new Button(container, SWT.PUSH);
		pimBrowse.setText("Browse");
		pimBrowse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				browse(pimText, "*.pim");
			}
		});
		
		Button pimLoad = new Button(container, SWT.PUSH);
		pimLoad.setText("Load");
		pimLoad.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				pimLoad();
			}});
		
		/* PMM */
		Label pmmLabel = new Label(container, SWT.NULL);
		pmmLabel.setText("&PMM:");
		
		pmmText = new Text(container, SWT.BORDER | SWT.SINGLE);
		
		Button pmmBrowse = new Button(container, SWT.PUSH);
		pmmBrowse.setText("Browse");
		pmmBrowse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				browse(pmmText, "*.pmm4bpmn");
			}
		});
		
		pmmLoad = new Button(container, SWT.PUSH);
		pmmLoad.setText("Load");
		pmmLoad.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				pmmLoad();
			}});
	
		/* OUT */
		Label outLabel = new Label(container, SWT.NULL);
		outLabel.setText("Out directory");
		
		outText = new Text(container, SWT.BORDER | SWT.SINGLE);
		
		Button outBrowse = new Button(container, SWT.PUSH);
		outBrowse.setText("Browse");
		outBrowse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				outBrowse();
			}
		});
		
		
		initialize();
		dialogChanged();
		setControl(container);
	}


	private void initialize() {
		pools= new ArrayList<Pool>();
	}

	private void bpmnLoad() {
		poolNames.clearSelection();
		pools.clear();
		
		Resource res=set.getResource(URI.createFileURI(bpmnText.getText()), true);
		if(res.isLoaded() && res.getContents().size()>0) {	
			diag=(BpmnDiagram) res.getContents().get(0);

			for(Pool pool:diag.getPools()) {
				poolNames.add(pool.getName());
				pools.add(pool);
			}
		}
		dialogChanged();
	}
	
	private void poolLoad() {
		int indice=poolNames.getSelectionIndex();
		bpd = null;

		Pool pool = pools.get(indice);
		try {
			bpd = BPDLoaderUtil.getInstance().load(pool);
		} catch (ProviderException e) {
			e.printStackTrace(); // TODO
		}

		dialogChanged();
	}
	
	private void pimLoad() {
		Resource res=set.getResource(URI.createFileURI(pimText.getText()), true);
		if(res.isLoaded() && res.getContents().size()>0) {	
			pim=(ProcessInformation) res.getContents().get(0);
		}
		
		dialogChanged();
	}
	
	private void pmmLoad() {
		
		Resource res=set.getResource(URI.createFileURI(pmmText.getText()), true);
		if(res.isLoaded() && res.getContents().size()>0) {	
			net.yienyien.process.pmm4bpmn.Bindings pmm4bpmn=(net.yienyien.process.pmm4bpmn.Bindings) res.getContents().get(0);	
			pmm=pmm4bpmn.toPmm(bpd);
		}
		dialogChanged();
	}

	private void browse(Text tgt, String filter) {
		if(tgt==null) {
			return;
		}
		
		FileDialog dialog=new FileDialog(getShell(), SWT.OPEN);
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

		dialog.setFilterPath(root.getRawLocation().toOSString());
		dialog.setFilterExtensions(new String[]{filter});
		
		String file=dialog.open();
		if(file!=null) {
			tgt.setText(file);
		}
	}
	
	private void outBrowse() {
		DirectoryDialog dialog=new DirectoryDialog(getShell());
		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

		
		dialog.setFilterPath(root.getRawLocation().toOSString());
		
		String file=dialog.open();
		if(file!=null) {
			outText.setText(file);
		}
	}
	

	
	/**
	 * Ensures that both text fields are set.
	 */

	private void dialogChanged() {
		if(bpd ==null) {
			updateStatus("Select a BPMN Model with a valid pool");
			pmmLoad.setEnabled(false);
			return;
		} 
		
		pmmLoad.setEnabled(true);
		
		if(pim ==null) {
			updateStatus("Select a valid PIM");
			return;
		}
		
		if(pmm ==null) {
			updateStatus("Select a valid PMM4BPMNl");
			return;
		}

		updateStatus(null);
	}
	
	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}


}